import 'package:english_words/english_words.dart';
import 'package:flutter/material.dart';

class HelloFlutter extends StatefulWidget {
  @override
  _HelloFlutterState createState() => _HelloFlutterState();
}

class _HelloFlutterState extends State<HelloFlutter> {
  int _count = 0;

  String _random = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('hello flutter 计数器案例'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          //两个加减按钮
          buildAddSubButton(),
          //文字显示
          Text('计数器$_count'),
          Text('生成一个随机字母$_random'),
        ],
      ),
      floatingActionButton: FloatingActionButton(
          onPressed: () {
            setState(() => _random = WordPair.random().toString());
          },
          child: Icon(Icons.print)),
    );
  }

  /*
  当按钮点击时，会调用此函数，该函数的作用是先自增_counter，然后调用setState 方法。setState方法的作用是通知Flutter框架，有状态发生了改变，
  Flutter框架收到通知后，会执行build方法来根据新的状态重新构建界面， Flutter 对此方法做了优化，使重新执行变的很快，所以你可以重新构建任何需要更新的东西，而无需分别去修改各个widget。
 */
  Row buildAddSubButton() {
    return //两个加减按钮
        Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
      RaisedButton(
        onPressed: () {
          setState(() {
            _count--;
          });
        },
        child: Text(
          '-',
          style: TextStyle(fontSize: 20),
        ),
        color: Colors.pink,
      ),
      RaisedButton(
        onPressed: () => setState(() => _count++),
        child: Text('+', style: TextStyle(fontSize: 20)),
        color: Colors.greenAccent,
      ),
    ]);
  }
}
